<template>
	<view class="page-wrapper">
		<view class="container">
			<!-- 头部欢迎区域 -->
			<view class="header">
			<view class="welcome-section">
				<text class="welcome-text">{{greeting}}，{{userName}}</text>
				<text class="subtitle">欢迎来到夕语时光馆</text>
			</view>
			<view class="logo-container">
				<view class="logo-frame">
					<view class="logo-content">
						<image src="/static/logo.jpg" mode="aspectFit"></image>
					</view>
				</view>
			</view>
		</view>

		<!-- 父母状态概览 -->
        <view class="parent-status">
            <view class="status-card">
                <view class="parent-info">
                    <text class="parent-name">父亲 - {{parentStatus.father.name}}</text>
                    <text class="status-text">今日心情：{{parentStatus.father.mood.emoji}} {{parentStatus.father.mood.level}}</text>
                </view>
                <view class="status-indicator" :class="parentStatus.father.indicator"></view>
            </view>
            <view class="status-card">
                <view class="parent-info">
                    <text class="parent-name">母亲 - {{parentStatus.mother.name}}</text>
                    <text class="status-text">今日心情：{{parentStatus.mother.mood.emoji}} {{parentStatus.mother.mood.level}}</text>
                </view>
                <view class="status-indicator" :class="parentStatus.mother.indicator"></view>
            </view>
        </view>

		<!-- 功能按钮区域 -->
		<view class="function-grid">
			<!-- 父母情绪日志 -->
			<view class="function-card mood-card" @tap="goToMoodLog">
				<view class="card-icon">
					<text class="icon">📝</text>
				</view>
				<text class="card-title">父母情绪</text>
				<text class="card-desc">查看情绪记录</text>
			</view>

			<!-- 用药提醒设置 -->
			<view class="function-card medicine-card" @tap="goToMedicineAlarm">
				<view class="card-icon">
					<text class="icon">⏰</text>
				</view>
				<text class="card-title">用药提醒</text>
				<text class="card-desc">设置提醒时间</text>
			</view>

			<!-- 设置 -->
			<view class="function-card settings-card" @tap="goToSettings">
				<view class="card-icon">
					<text class="icon">⚙️</text>
				</view>
				<text class="card-title">设置</text>
				<text class="card-desc">管理父母信息</text>
			</view>
		</view>
		</view>
	
	<!-- 底部导航栏 -->
	<view class="tabbar">
		<view class="tabbar-item" :class="{ active: currentTab === 'home' }" @tap="switchTab('home')">
			<image class="tabbar-icon" :src="currentTab === 'home' ? '/static/shouye-active.png' : '/static/shouye.png'" mode="aspectFit"></image>
			<text class="tabbar-text">首页</text>
		</view>
		<view class="tabbar-item" :class="{ active: currentTab === 'location' }" @tap="switchTab('location')">
			<image class="tabbar-icon" :src="currentTab === 'location' ? '/static/ditu-active.png' : '/static/ditu.png'" mode="aspectFit"></image>
			<text class="tabbar-text">查看位置</text>
		</view>
		<view class="tabbar-item" :class="{ active: currentTab === 'settings' }" @tap="switchTab('settings')">
			<image class="tabbar-icon" :src="currentTab === 'settings' ? '/static/wode-active.png' : '/static/wode.png'" mode="aspectFit"></image>
			<text class="tabbar-text">设置</text>
		</view>
	</view>
	</view>
</template>

<script>
    // 尝试使用相对路径导入
    import apiService from '../../utils/api.js';
    export default {
	data() {
		return {
			userName: '小明',
			greeting: '',
			currentTab: 'home', // 当前选中的tab
			parentStatus: {
				father: { name: '张建国', mood: { emoji: '😊', level: '良好', emotion: '开心' }, indicator: 'good' },
				mother: { name: '李秀英', mood: { emoji: '😐', level: '一般', emotion: '平静' }, indicator: 'normal' }
			}
		}
	},
		onLoad() {
			this.getGreeting();
			this.loadParentData();
		},
		methods: {
			// 获取问候语
			getGreeting() {
				const hour = new Date().getHours();
				if (hour < 12) {
					this.greeting = '早上好';
				} else if (hour < 18) {
					this.greeting = '中午好';
				} else {
					this.greeting = '晚上好';
				}
			},
			
            // 加载父母今日情绪（实时）
            async loadParentData() {
                try {
                    // 检查apiService是否存在且可用
                    if (apiService && typeof apiService.getTodayMood === 'function') {
                        const fatherMood = await apiService.getTodayMood('father');
                        const motherMood = await apiService.getTodayMood('mother');
                        this.parentStatus.father.mood = fatherMood;
                        this.parentStatus.mother.mood = motherMood;
                        this.parentStatus.father.indicator = fatherMood.level === '良好' ? 'good' : fatherMood.level === '一般' ? 'normal' : 'low';
                        this.parentStatus.mother.indicator = motherMood.level === '良好' ? 'good' : motherMood.level === '一般' ? 'normal' : 'low';
                    } else {
                        console.log('apiService不可用，使用默认数据');
                        // 已经有默认数据在data中，保持不变
                    }
                } catch (e) {
                    console.error('加载今日情绪失败', e);
                    // 出错时保持默认数据
                }
            },
			
			// 跳转到父母情绪日志页面
			goToMoodLog() {
				uni.navigateTo({
					url: '/pages/mood-log/mood-log'
				});
			},
			
			// 跳转到用药提醒设置页面
			goToMedicineAlarm() {
                uni.navigateTo({ url: '/pages/medicine-alarm/medicine-alarm' });
			},
			
	
			
		// 跳转到设置页面
		goToSettings() {
			uni.showToast({
				title: '设置功能正在开发中',
				icon: 'none',
				duration: 2000
			});
		},
		
		// 切换底部导航栏
		switchTab(tab) {
			this.currentTab = tab;
			
			if (tab === 'home') {
				// 当前就是首页，不需要操作
			} else if (tab === 'location') {
				// 查看位置
				uni.showToast({
					title: '查看父母位置',
					icon: 'none'
				});
			} else if (tab === 'settings') {
				// 设置
				uni.showToast({
					title: '设置功能正在开发中',
					icon: 'none',
					duration: 2000
				});
			}
		}
		}
	}
</script>

<style>
.page-wrapper {
	height: 100vh;
	position: relative;
	overflow: hidden;
}

.container {
	height: 100%;
	background: linear-gradient(135deg, #f9f7f7 0%, #eaf4f4 100%);
	padding: 0 30rpx;
	padding-bottom: 120rpx;
	box-sizing: border-box;
	overflow-y: auto;
	animation: pageLoad 0.6s ease-out;
}

	@keyframes pageLoad {
		0% {
			opacity: 0;
			transform: translateY(20rpx);
		}
		100% {
			opacity: 1;
			transform: translateY(0);
		}
	}

	/* 头部区域 */
	.header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 40rpx 0 20rpx 0;
	}

	.welcome-section {
		flex: 1;
	}

	.welcome-text {
		display: block;
		font-size: 48rpx;
		font-weight: bold;
		color: #3d5a80;
		margin-bottom: 10rpx;
	}

	.subtitle {
		font-size: 28rpx;
		color: #5c6b73;
	}

	/* Logo 样式 */
	/* 头部右侧图片整体尺寸与布局 */ 
.logo-container {
	width: 120rpx;          /* 调整展示尺寸 */
	height: 120rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

/* 外层卡片：圆角+阴影+裁切 */
.logo-frame {
		width: 100%;
		height: 100%;
		border-radius: 18rpx;
		background: #ffffff;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
		overflow: hidden;
		display: flex;
		align-items: center;
		justify-content: center;
	}

/* 内容容器：留点内边距让图片更呼吸 */
.logo-content {
	width: 80%;
	height: 80%;
	padding: 0rpx;         /* 需要更满可减小或设为0 */
	box-sizing: border-box;
	display: flex;
	align-items: center;
	justify-content: center;
}

/* 图片本身：保持比例自适应，不变形不裁切 */
.logo-content image {
	width: 100%;
	height: 100%;
	object-fit: contain;    /* H5/小程序通用；保持比例完整展示 */
}


	/* 父母状态概览 */
	.parent-status {
		margin-bottom: 20rpx;
		opacity: 0;
		animation: fadeIn 0.5s ease-out 0.2s forwards;
	}

	@keyframes fadeIn {
		0% {
			opacity: 0;
			transform: translateY(10rpx);
		}
		100% {
			opacity: 1;
			transform: translateY(0);
		}
	}

	.status-card {
		background: #ffffff;
		border-radius: 16rpx;
		padding: 20rpx 30rpx;
		margin-bottom: 12rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
		transition: all 0.3s ease;
	}

	.status-card:hover {
		transform: translateY(-2rpx);
		box-shadow: 0 6rpx 20rpx rgba(0, 0, 0, 0.1);
	}

	.status-card:active {
		transform: scale(0.98);
	}

	.parent-info {
		flex: 1;
	}

	.parent-name {
		display: block;
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 8rpx;
	}

	.status-text {
		font-size: 26rpx;
		color: #666;
	}

	.status-indicator {
		width: 20rpx;
		height: 20rpx;
		border-radius: 50%;
	}

	.status-indicator.good {
		background: #4CAF50;
	}

	.status-indicator.normal {
		background: #FF9800;
	}

	/* 功能按钮网格 */
	.function-grid {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		flex-wrap: nowrap;
		margin-bottom: 20rpx;
		opacity: 0;
		animation: fadeIn 0.5s ease-out 0.4s forwards;
	}

	.function-card {
		/* background: rgba(255, 255, 255, 0.9);
		border-radius: 24rpx;
		padding: 40rpx 30rpx;
		text-align: center;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
		transition: all 0.3s ease;
		position: relative;
		overflow: hidden;
		flex: 1;
		margin: 0 10rpx;
		min-width: 200rpx;
		backdrop-filter: blur(10rpx); */
	  width: 30%; /* 一行三列，每列占30%左右（可根据设计微调） */
	  display: flex;
	  flex-direction: column; /* 卡片内图标、标题、描述纵向排列 */
	  align-items: center; /* 内容水平居中 */
	  justify-content: center; /* 内容垂直居中 */
	  /* 可添加背景、圆角、阴影等样式，例如： */
	  background-color: #fff;
	  border-radius: 8px;
	  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
	  padding: 20rpx 10rpx;
	  box-sizing: border-box; /* 避免padding撑大宽度 */
	  position: relative;
	}

	.function-card::before {
		content: '';
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		height: 6rpx;
		background: linear-gradient(90deg, #ff6b6b, #4ecdc4);
	}

	.function-card:active {
		transform: scale(0.95);
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.2);
	}

	.function-card:hover {
		transform: translateY(-3rpx);
		box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.12);
	}

	.card-icon {
		margin-bottom: 10rpx;
	}

	.icon {
		font-size: 48rpx;
		display: block;
	}

	.card-title {
		display: block;
		font-size: 26rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 4rpx;
	}

	.card-desc {
		font-size: 15rpx;
		color: #666;
	}

	/* 不同卡片的特殊样式 */
	.mood-card::before {
		background: linear-gradient(90deg, #f8b195, #f67280);
	}

	.medicine-card::before {
		background: linear-gradient(90deg, #66bb6a, #43a047);
	}

	.health-card::before {
		background: linear-gradient(90deg, #4fc3f7, #29b6f6);
	}

	.settings-card::before {
		background: linear-gradient(90deg, #9575cd, #7e57c2);
		
	}

	/* 底部导航栏 */
	.tabbar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		width: 100%;
		height: 120rpx;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		background: #ffffff;
		border-top: 1rpx solid #e5e5e5;
		display: flex;
		justify-content: space-around;
		align-items: flex-start;
		box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
		z-index: 9999;
	}

	.tabbar-item {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: flex-start;
		padding-top: 12rpx;
		padding-bottom: 8rpx;
		transition: all 0.3s ease;
	}

	.tabbar-icon {
		width: 44rpx;
		height: 44rpx;
		margin-bottom: 8rpx;
		transition: all 0.3s ease;
	}

	.tabbar-text {
		font-size: 24rpx;
		color: #999;
		transition: color 0.3s ease;
	}

	.tabbar-item.active .tabbar-text {
		color: #3d5a80;
		font-weight: bold;
	}

	.tabbar-item:active {
		transform: scale(0.95);
	}

</style>
